home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_313 / uucp / uucp1.lzh / src / lib / security.c < prev    next >
C/C++ Source or Header  |  1990-01-14  |  2KB  |  104 lines

  1.  
  2. /*
  3.  *  SECURITY.C
  4.  *
  5.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  6.  *
  7.  *  Checks whether a given file should be allowed to be read or written
  8.  *
  9.  *  Lock directory containing file
  10.  *  Generate directory path
  11.  *  Check path against allowed list (UULIB:Security)
  12.  *
  13.  *  If type == 'c' return  1 if file name begins with a C.
  14.  *           return -1 if file name does not begin with a C.
  15.  *           return  0 if file name begins with a C. but is for
  16.  *            a directory other than the current one.
  17.  */
  18.  
  19. #include <proto/all.h>
  20. #include <libraries/dosextens.h>
  21. #include <stdio.h>
  22.  
  23. typedef struct FileLock FileLock;
  24.  
  25. static char TmpBuf[128];
  26.  
  27. SecurityDisallow(file, type)
  28. char *file;
  29. {
  30.     char *fs;
  31.     char c;
  32.     int  disallow = 1;
  33.     BPTR lock;
  34.     BPTR slock;
  35.  
  36.     for (fs = file + strlen(file); fs >= file && *fs != '/' && *fs != ':'; --fs);
  37.     ++fs;
  38.     if (fs == file) {           /*  just a file name    */
  39.     if (type == 'c') {
  40.         if ((file[0]|0x20) == 'c' && file[1] == '.')
  41.         return(1);
  42.         return(-1);
  43.     }
  44.     return(0);              /*  type r or w, current dir, allow. */
  45.     }
  46.     if (type == 'c')            /*  not just a file name, allow C.   */
  47.     return(0);
  48.     c = *fs;
  49.     *fs = 0;        /*  keep just the path        */
  50.  
  51.     lock = Lock(file, SHARED_LOCK);
  52.     if (lock) {
  53.     FILE *fi = fopen("UULIB:Security", "r");
  54.     if (fi) {
  55.         while (fgets(TmpBuf, sizeof(TmpBuf), fi)) {
  56.         char *ptr;
  57.         if (TmpBuf[0] == '#' || TmpBuf[0] == '\n' || TmpBuf[0] == 0)
  58.             continue;
  59.  
  60.         /*
  61.          *  breakout the directory name and permissions
  62.          */
  63.  
  64.         for (ptr = TmpBuf; *ptr != '\n' && *ptr != ' ' && *ptr != 9; ++ptr);
  65.         *ptr = 0;
  66.  
  67.         /*
  68.          *  permissions allowed?
  69.          */
  70.  
  71.         for (++ptr; *ptr && *ptr != type; ++ptr);
  72.         if (*ptr == 0)      /*  sorry   */
  73.             continue;
  74.  
  75.         if (slock = Lock(TmpBuf, SHARED_LOCK)) {
  76.             if (SameLock(lock, slock))
  77.             disallow = 0;
  78.             UnLock(slock);
  79.         }
  80.         if (disallow == 0)
  81.             break;
  82.         }
  83.         fclose(fi);
  84.     }
  85.     UnLock(lock);
  86.     }
  87.  
  88.     *fs = c;        /*  restore path    */
  89.  
  90.     return(disallow);
  91. }
  92.  
  93. SameLock(lock, slock)
  94. BPTR lock, slock;
  95. {
  96.     FileLock *fl1 = (FileLock *)((long)lock << 2);
  97.     FileLock *fl2 = (FileLock *)((long)slock << 2);
  98.  
  99.     if (fl1->fl_Task == fl2->fl_Task && fl1->fl_Key == fl2->fl_Key)
  100.     return(1);
  101.     return(0);
  102. }
  103.  
  104.